import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_study/model/issue_model.dart';
import 'package:flutter_study/page/video_detail_page.dart';
import 'package:flutter_study/util/date_util.dart';
import 'package:flutter_study/util/navigator_manager.dart';
import 'package:flutter_study/util/share_util.dart';

import 'home_page.dart';

class RankWidgetItem extends StatelessWidget {
  final Item item;
  final bool showCategory;
  final bool showDivider;

  const RankWidgetItem(
      {Key key, this.item, this.showCategory = true, this.showDivider = true})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        GestureDetector(
          onTap: () {
            NavigatorManager.to(VideoDetailPage(data: item.data));
          },
          child: Padding(
            padding: EdgeInsets.fromLTRB(15, 10, 15, 10),
            child: Stack(
              children: [
                ClipRRect(
                  child: Hero(
                    tag: "${item.data.id}${item.data.time}",
                    child: CachedNetworkImage(
                      fit: BoxFit.cover,
                      width: MediaQuery.of(context).size.width,
                      height: 200,
                      imageUrl: item.data.cover.feed,
                      errorWidget: (context, url, error) {
                        return Image.asset("images/img_load_fail.png");
                      },
                    ),
                  ),
                  borderRadius: BorderRadius.circular(4),
                ),
                Positioned(
                  left: 15,
                  top: 10,
                  child: Opacity(
                    opacity: showCategory ? 1.0 : 0.0,
                    child: ClipOval(
                      child: Container(
                        decoration: BoxDecoration(color: Colors.white54),
                        height: 44,
                        width: 44,
                        alignment: Alignment.center,
                        child: Text(
                          item.data.category,
                          style: TextStyle(color: Colors.white, fontSize: 14),
                        ),
                      ),
                    ),
                  ),
                ),
                Positioned(
                  right: 15,
                  bottom: 10,
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(5),
                    child: Container(
                      decoration: BoxDecoration(color: Colors.black54),
                      padding: EdgeInsets.all(5),
                      child: Text(
                        DateUtils.formatDateMsByMs(item.data.duration * 1000),
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: 12,
                            fontWeight: FontWeight.bold),
                      ),
                    ),
                  ),
                )
              ],
            ),
          ),
        ),
        Container(
          decoration: BoxDecoration(color: Colors.white),
          padding: EdgeInsets.fromLTRB(15, 0, 15, 10),
          child: Row(
            children: [
              _authorHeaderImage(item),
              Expanded(
                flex: 1,
                child: Container(
                  padding: EdgeInsets.only(left: 10),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        item.data.title,
                        style: TextStyle(
                            color: Colors.black,
                            fontSize: 14,
                            fontWeight: FontWeight.bold),
                        overflow: TextOverflow.ellipsis,
                      ),
                      Padding(
                        padding: EdgeInsets.only(top: 2),
                        child: Text(
                          item.data.author == null
                              ? item.data.tags[0].name
                              : item.data.author.name,
                          style:
                              TextStyle(color: Color(0xff9a9a9a), fontSize: 12),
                        ),
                      )
                    ],
                  ),
                ),
              ),
              IconButton(
                  icon: Icon(
                    Icons.share,
                    color: Colors.black38,
                  ),
                  onPressed: () {
                    ShareUtil.share(item.data.title, _getVideoShareUrl());
                  })
            ],
          ),
        ),
        Offstage(
          offstage: showDivider,
          child: Padding(
            padding: EdgeInsets.only(left: 15, right: 15),
            child: Divider(
              height: 0.5,
            ),
          ),
        )
      ],
    );
  }

  _authorHeaderImage(Item item) {
    if (item.data.author == null) {
      return ClipRRect(
        clipBehavior: Clip.antiAlias,
        child: CachedNetworkImage(
          width: 40,
          height: 40,
          imageUrl: item.data.tags[0].headerImage,
          fit: BoxFit.cover,
        ),
      );
    } else {
      return ClipOval(
        child: CachedNetworkImage(
          width: 40,
          height: 40,
          imageUrl: item.data.author.icon,
        ),
      );
    }
  }

  _getVideoShareUrl() {
    String videoUrl;
    var playInfo = item.data.playInfo;
    if (playInfo.length > 1) {
      playInfo.forEach((element) {
        if (element.type == VIDEOTYPE) {
          videoUrl = element.url;
          return videoUrl;
        }
      });
    } else {
      videoUrl = item.data.playUrl;
      return videoUrl;
    }
  }
}
